﻿#ifndef VIEWER_H
#define VIEWER_H

#include <QMainWindow>
#include <QtQuickWidgets/QQuickWidget>
#include <QtQmlDebug/QtQmlDebug>
#include <QList>
#include <QVector>
#include <QString>
#include <QtSql/QSqlDatabase>
#include <QCameraInfo>
#include <QCameraViewfinder>
#include <QMessageBox>
#include <QPainter>
#include <QLineF>
#include <led.h>
#include <QTimer>
#include <QThread>
#include <QMap>
#include <QQmlProperty>
#include "paintitem.h"
#include <QObject>
#include <functional>
#include <QThreadPool>
#include <QEventLoop>
#include <QLabel>
#include <QLayout>
#include <QDialogButtonBox>
#include <QQmlApplicationEngine>
#undef PATTERN

enum DIRECT{
    UP=1,
    DOWN=2,
    FORWARD=3,
    BACK=4,
    LEFT=5,
    RIGHT=6,
    UPFORWARD,
    UPBACK,
    UPLEFT,
    UPRIGHT,
    DOWNFORWARD,
    DOWNBACK,
    DOWNLEFT,
    DOWNRIGHT,
    ORIGIN
};

enum{
    MOVE=3
};

enum STATUS{
    POWERON=1,
    PLACED,
    START,
    PAT,
    LOOSE,
    STOP
};

class Viewer: public QObject
{
    Q_OBJECT
public:
    Viewer(QQmlApplicationEngine& engine): qmlRootObj(engine.rootObjects().first()),
                                                    vecScrewLeds(6+1,QVector<Led*>(4+1,nullptr)),
                                                    vecOpLeds(10+1,nullptr),
                                                    vecArrows(6+1,nullptr),
                                                    literal(nullptr),
                                                    vecStatus(6+1, {nullptr, nullptr}),
                                                    infoBox(new QMessageBox)
    {
        init();
    }
    void bindPanelState(DIRECT& direct);
    void setStatusLight(int which);
    void setStatusDark(int which);
    void switchPattern(bool manualPattern);
    void init()
    {
        initLeds();
        initPanel();
        initStatusBar();
        initVideo();
        initInfoBox();
    }
    void ledSelfCheck(QEventLoop*);
    void submit();
    void initInfoBox(){
        infoBox->setStyleSheet("QLabel{"
                               "min-width: 500px;"
                               "min-height: 300px; "
                               "font-size: 60px;"
                               "}"
                               "QDialogButtonBox{"
                               "min-width: 120px;"
                               "min-height: 70px; "
                               "font-size: 30px;"
                               "}"
                               "QPushButton {"
                               "font-size: 24px;"
                               "min-width: 120px;"
                               "min-height: 65px;"
                               "}"
                               );
        infoBox->setStandardButtons(QMessageBox::Yes | QMessageBox::No);
        infoBox->setDefaultButton(QMessageBox::Yes);
        QLabel* iconLabel = infoBox->findChild<QLabel*>("qt_msgboxex_icon_label");
        iconLabel->deleteLater();

        QLabel* label = infoBox->findChild<QLabel*>("qt_msgbox_label");
        label->setAlignment(Qt::AlignCenter);

        QDialogButtonBox* buttonBox = infoBox->findChild<QDialogButtonBox*>("qt_msgbox_buttonbox");
        buttonBox->setCenterButtons(true);
    }
private:
    void initVideo();
    void initLeds();
    void initStatusBar();
    void initPanel();
    void openCamera();
    void closeCamera();
    void updatePanelState(DIRECT*direct,DIRECT*predirect);
    ~Viewer();

public:
    // 摄像头
    QList<QCameraInfo> devInfos;
    QCamera* ca;
    QObject* qmlRootObj;

    // 指示灯
    QVector<QVector<Led*>> vecScrewLeds;
    QVector<Led*> vecOpLeds;

    // 方向盘
    QVector<PaintItem*> vecArrows;
    QObject* literal;

    // 状态栏
    QVector<std::pair<QQuickItem*, QQuickItem*>> vecStatus;

    // 消息弹窗
    QMessageBox* infoBox;
};

#endif // VIEWER_H
